package org.dfl.plugin.editor.contributionitems;

import org.dfl.plugin.editor.actions.RunAction;
import org.dfl.plugin.editor.actions.StopAction;
import org.dfl.plugin.editor.model.DflDiagram;
import org.eclipse.jface.action.IAction;
import org.eclipse.swt.widgets.ToolItem;

/**
 * contribution item for RunAction action
 * @author Piotr Wlodarczyk
 *
 */
public class RunActionContributionItem extends FlowRunnerActionContributionItem {
	private IAction runAction;
	private IAction stopAction;
	
	@Override
	public void setDiagram(DflDiagram diagram) {
		runAction = new RunAction(null,diagram);
		stopAction = new StopAction(null,diagram);
		super.setDiagram(diagram);
	}

	@Override
	protected void itemSelected() {
		if(runAction.isEnabled()){
			runAction.run();
		}else{
			stopAction.run();
		}
	}

	@Override
	protected void updateItem(ToolItem item) {
		if(item!=null){
			if(diagram==null){
				setRunAction(item,false);
			}else if(runAction.isEnabled()){
				setRunAction(item,true);
			}else if(stopAction.isEnabled()){
				setStopAction(item,true);
			}else{
				setRunAction(item,false);
			}
		}	
	}
	
	private void setRunAction(ToolItem item,boolean enabled){
		item.setImage(runAction.getImageDescriptor().createImage());
		item.setToolTipText(runAction.getToolTipText());
		item.setEnabled(enabled);
	}
	
	private void setStopAction(ToolItem item,boolean enabled){
		item.setImage(stopAction.getImageDescriptor().createImage());
		item.setToolTipText(stopAction.getToolTipText());
		item.setEnabled(enabled);
	}
}
